package com.caiheng.api.controller.platform;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caiheng.api.constant.ResponseCode;
import com.caiheng.api.entity.sys.SysNotice;
import com.caiheng.api.entity.sys.SysNoticeGroup;
import com.caiheng.api.entity.wxuser.User;
import com.caiheng.api.service.estate.EstateService;
import com.caiheng.api.service.sys.ISysNoticeGroupService;
import com.caiheng.api.service.sys.SysConfigService;
import com.caiheng.api.service.sys.SysNoticeService;
import com.caiheng.api.service.wxuser.UserService;
import com.caiheng.api.util.GsonResultBean;
import com.caiheng.api.util.Tools;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *@Author: ZhouWei
 *@Description:  通知公告类 相关接口
 *@Date 2018-05-10
 */
@Component
@RestController
@RequestMapping("/notice")//url:模块/资源/{}/细分
public class NoticeController extends BaseController {

    @Resource
    private UserService userService;
    @Resource
    private EstateService estateService;
    @Resource
    private SysConfigService sysConfigService;
    @Resource
    private SysNoticeService noticeService;
    @Resource
    private ISysNoticeGroupService sysNoticeGroupService;

    @Value("${web.imgPrefixOss}")
    private String imgPrefix;//图片地址

    /**
     * 小区详情
     * @param
     * @throws Exception
     */
    @PostMapping(value="/detail")
    public JSONObject detail(@RequestBody String requestParam) throws Exception{
        JSONObject result = null;
        try{
            if(StrUtil.isEmpty(requestParam)){
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"参数异常"),true,true);
            }
            logBefore(logger, "notice/detail 接收的参数： "+requestParam);
            System.out.println("notice/detail 接收的参数： " + requestParam);
            JSONObject jsonRequest = JSONUtil.parseObj(requestParam);
            int user_id = jsonRequest.containsKey("user_id") ? jsonRequest.getInt("user_id") : 0;
            int notice_id = jsonRequest.containsKey("notice_id") ? jsonRequest.getInt("notice_id") : 0;

//            User user = userService.findById(user_id);
//            if(user == null){
//                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"当前用户不存在"),true,true);
//            }
            SysNotice notice = noticeService.findById(notice_id);
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.OK,"成功",notice),true,false);
        }catch (Exception e){
            e.printStackTrace();
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.ERROR,"系统异常",e.getMessage()),true,true);
            System.out.println(e.getMessage());
            logger.error(e.getMessage(), e);
        }
        return result;
    }

    /**
     *  通知公告分组列表
     * @param
     * @throws Exception
     */
    @PostMapping(value="/group/list")
    public JSONObject groupList(@RequestBody String requestParam) throws Exception{
        JSONObject result = null;
        Map<String, Object> map =  new HashMap<>();
        try{
            if(StrUtil.isEmpty(requestParam)){
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"参数异常"),true,true);
            }
            logBefore(logger, "notice/list 接收的参数： "+requestParam);
            System.out.println("notice/list 接收的参数： "+requestParam);
            JSONObject jsonRequest = JSONUtil.parseObj(requestParam);
            int user_id = jsonRequest.containsKey("user_id") ? jsonRequest.getInt("user_id") : 0;
            int estateId = jsonRequest.containsKey("estateId") ? jsonRequest.getInt("estateId") : 0;

            User user = userService.findById(user_id);
            if(user == null){
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"当前用户不存在"),true,true);
            }


            QueryWrapper<SysNoticeGroup> qw = new QueryWrapper<>();
            qw.eq("parent_id", 0);
            qw.eq("state", 1);
            List<SysNoticeGroup> groupList = sysNoticeGroupService.list(qw);
            for (SysNoticeGroup group : groupList){
                if(StrUtil.isNotEmpty(group.getLogo())){
                    group.setLogo(imgPrefix + group.getLogo());
                }
                QueryWrapper<SysNoticeGroup> qwChild = new QueryWrapper<>();
                qwChild.eq("parent_id", group.getId());
                qwChild.eq("state", 1);
                List<SysNoticeGroup> childList = sysNoticeGroupService.list(qwChild);
                group.setChildList(childList);
            }
            map.put("groupList", groupList);
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.OK,"成功",groupList.size(),map),false,false);
        }catch (Exception e){
            e.printStackTrace();
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.ERROR,"系统异常",e.getMessage()),true,true);
            System.out.println(e.getMessage());
            logger.error(e.getMessage(), e);
        }
        return result;
    }


    /**
     *  通知公告列表
     * @param
     * @throws Exception
     */
    @PostMapping(value="/list")
    public JSONObject list(@RequestBody String requestParam) throws Exception{
        JSONObject result = null;
        try{
            if(StrUtil.isEmpty(requestParam)){
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"参数异常"),true,true);
            }
            logBefore(logger, "notice/list 接收的参数： "+requestParam);
            System.out.println("notice/list 接收的参数： "+requestParam);
            JSONObject jsonRequest = JSONUtil.parseObj(requestParam);
            int user_id = jsonRequest.containsKey("user_id") ? jsonRequest.getInt("user_id") : 0;
            int notice_type = jsonRequest.containsKey("notice_type") ? jsonRequest.getInt("notice_type") : -1;
            int pageIndex = jsonRequest.containsKey("pageIndex")&& StrUtil.isNotEmpty(jsonRequest.get("pageIndex").toString()) ? jsonRequest.getInt("pageIndex") : 1;
            int pageSize = jsonRequest.containsKey("pageSize")&& StrUtil.isNotEmpty(jsonRequest.get("pageSize").toString()) ? jsonRequest.getInt("pageSize") : 10;
            int groupId = jsonRequest.containsKey("groupId") ? jsonRequest.getInt("groupId") : 0;
            int estateId = jsonRequest.containsKey("estateId") ? jsonRequest.getInt("estateId") : 0;

            User user = userService.findById(user_id);
            if(user == null){
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"当前用户不存在"),true,true);
            }

            List<SysNotice> notices = noticeService.getListByType(user,notice_type,groupId,estateId,pageIndex,pageSize);
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.OK,"成功",notices.size(),notices),false,false);
        }catch (Exception e){
            e.printStackTrace();
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.ERROR,"系统异常",e.getMessage()),true,true);
            System.out.println(e.getMessage());
            logger.error(e.getMessage(), e);
        }
        return result;
    }


}
